﻿using AutoMapper;
using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;
using WarehouseManagement.Application.Command.ConfirmTheOutboundCommand;
using WarehouseManagement.Domain.DTO;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.ConfirmTheOutboundCommandHandlers
{
    public class ConfirmTheOutboundCreateCommandHandlers : IRequestHandler<ConfirmTheOutboundCreateCommand, int>
    {
        protected readonly IOutboundRequestRepository outbound;// 出库请求仓储接口
        protected readonly IOutboundRProductRepository outboundR; // 仓库出库申请产品关系仓储接口
        protected readonly ISqlSugarClient db;

        public ConfirmTheOutboundCreateCommandHandlers(IOutboundRequestRepository outbound, IOutboundRProductRepository outboundR, ISqlSugarClient db)
        {
            this.outbound = outbound;
            this.outboundR = outboundR;
            this.db = db;
        }

        public async Task<int> Handle(ConfirmTheOutboundCreateCommand request, CancellationToken cancellationToken)
        {
            foreach (var item in request.dtos)
            {
                var newout = (await outboundR.GetAll()).First(x => x.OutboundRProductId == item.OutboundRProductId);
                OutboundRProduct outR = new OutboundRProduct
                {
                    AddName = item.AddName,
                    Office_Id = item.Office_Id,
                    OutboundNumber = item.OutboundNumber,
                    AddTime = item.AddTime,
                    DelName = item.DelName,
                    DelTime = item.DelTime,
                    IsDel = item.IsDel,
                    ORQPrice = item.ORQPrice,
                    ORQuantityNum = item.ORQuantityNum,
                    OutboundRProductId = item.OutboundRProductId,
                    OUTnum = item.OUTnum,
                    ProductId = item.ProductId,
                    UpdName = item.UpdName,
                    UpdTime = item.UpdTime,
                    User_Id = item.User_Id
                };
                await outboundR.UpdateAsync(outR);
                MessageCreateDto dto = new MessageCreateDto();
                dto.MType = "确认出库";
                dto.Title = "确认出库新增";
                dto.SendTime = DateTime.Now;
                await MessageCreates(dto);
                if (item.ORQuantityNum == item.OUTnum)
                {
                    var outlist = (await outbound.GetAll()).First(x => x.OutboundNumber == item.OutboundNumber);
                    outlist.ORIsState = 2;
                    await outbound.UpdateAsync(outlist);
                }
                else
                {
                    var outlist = (await outbound.GetAll()).First(x => x.OutboundNumber == item.OutboundNumber);
                    outlist.ORIsState = 3;
                    await outbound.UpdateAsync(outlist);
                }
            }
            return 1;
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public async Task<int> MessageCreates(MessageCreateDto dtos)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://116.205.121.197:8000/erpcp/api/Message/SendMessage";
            string jsonstr = JsonConvert.SerializeObject(dtos);
            string postReponse = await cilent.PostAsync(chanpin, jsonstr);
            return 1;
        }
    }
}
